<%@page import="edu.facilites.model.User"%>
<%@page import="edu.facilities.utils.Format"%>
<%@page import="edu.facilites.model.Grade"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>体育设施预约系统</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <script src="js/jquery-1.6.2.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#_selectuser").click(function () {
                    $("#_type").val("select");//查询按钮点击后类型为select
                    $("#_form1").submit();
                });
                $("#_adduser").click(function () {
                    $("#_type").val("add");//添加用户按钮点击后类型为add
                    $("#_adddiv").addClass("r-1-bot");
                    $("#_adddiv").show();
                });
                var usertype = "<%=Format.null2Blank(request.getAttribute("usertype"))%>";
                $("select[@name='_usertype'] option").each(function() {
                   if($(this).text() == usertype) {
                       $(this).attr("selected", "selected");
                   } 
                });
                var type = "<%=Format.null2Blank(request.getAttribute("type"))%>";
                if(type == "edit" || type == 'add') {
                    $("#_adddiv").addClass("r-1-bot");
                    $("#_adddiv").show();
                }
                $("#_submit").click(function (){
                    
                });
            });
            function edit(_id) {
                $("#_type").val("edit");//添加用户按钮点击后类型为edit
                $("#_userid").val(_id);
                $("#_form1").submit();
            }
            function del(_id) {
                $("#_type").val("del");//添加用户按钮点击后类型为edit
                $("#_userid").val(_id);
                $("#_form1").submit();
            }
        </script>
    </head>

    <body>
        <h1>
            <a>体育设施预约系统</a>
        </h1>
        <p class="man">您好：<%=request.getSession().getAttribute("userName")%></p>
        <div class="man-list">
            <jsp:include page="/WEB-INF/jsp/leftFrame.jsp" />
            <!-- InstanceBeginEditable name="EditRegion1" --> 
            <form id="_form1" action="<%=request.getContextPath()%>/userManager.do" method="post">
                <p class="r-1">
                    <span>班级：</span><select id="_grade" name="_grade">
                        <%
                            try {
                                List<Grade> list = (List<Grade>) request.getAttribute("gradelist");
                                StringBuilder sb = new StringBuilder();
                                int gradeid = Format.str2Int(request.getAttribute("gradeid"));
                                for (Grade grade : list) {
                                    sb.append("<option value='");
                                    sb.append(grade.getId());
                                    if(grade.getId() == gradeid) {
                                        sb.append("' selected='selected");
                                    }
                                    sb.append("'>");
                                    sb.append(grade.getName());
                                    sb.append("</>");
                                }
                                out.print(sb.toString());
                            } catch (Exception e) {
                            }
                        %>
                    </select>
                    <input type="hidden" id="_type" name="_type" value="<%=request.getAttribute("type")%>" />
                    <span>用户类型：</span><select name="_usertype">
                        <option value="2">操作员</option>
                        <option value="3">老师</option>
                        <option value="4">学生</option>
                    </select>
                    <input type="button" id="_selectuser" value="查询" style="margin-left: 10px;width:80px;height: 40px;"/>
                    <input type="button" id="_adduser" value="添加新用户" style="margin-left: 30px;width:80px;height: 40px;"/>
                </p>
                <div id='_adddiv' style='display: none;'>
                    <ul>
                        <%User user = null; 
                        try{
                              user = (User)request.getAttribute("user");
                           }catch(Exception e){
                           }
                        if(null == user) {
                            user = new User();
                        }
                        %>
                        <input type="hidden" name="_userid" id="_userid" value="<%=user.getId()%>"/>
                        <li>帐户：<input type="text" name="account" maxlength="50" value="<%=Format.null2Blank(user.getAccount())%>"/></li>
                        <li>密码：<input type="text" name="password" maxlength="50" value="<%=Format.null2Blank(user.getPassword())%>"/></li>
                        <li>姓名：<input type="text" name="name" maxlength="50" value="<%=Format.null2Blank(user.getName())%>"/></li>
                        <li>缺席次数：<input type="text" name="absenceNum" value="<%=user.getAbsenceNum()%>"></li>
                        <li>缺席日期：<input type="text" name="absenceDate" value="<%=Format.null2Blank(user.getAbsenceDate())%>"></li>
                        <li>用户类型：<select name="_usertype1">
                            <option value="2" <%String usertype = user.getUserTypeID() == 2 ? "selected='selected'" : "";%>>操作员</option>
                            <option value="3" <%usertype = user.getUserTypeID() == 3 ? "selected='selected'" : "";%>>老师</option>
                            <option value="4" <%usertype = user.getUserTypeID() == 4 ? "selected='selected'" : "";%>>学生</option>
                        </select></li>
                        <li>班级：<select name="_grade1">
                            <%
                                try {
                                    List<Grade> list1 = (List<Grade>) request.getAttribute("gradelist");
                                    StringBuilder sb1 = new StringBuilder();
                                    for (Grade grade : list1) {
                                        sb1.append("<option value='");
                                        sb1.append(grade.getId() == user.getGradeID() ? (grade.getId() + "' selected='selected") : grade.getId());
                                        sb1.append("'>");
                                        sb1.append(grade.getName());
                                        sb1.append("</>");
                                    }
                                    out.print(sb1.toString());
                                } catch (Exception e) {
                                }
                            %>
                        </select></li>
                        <li>是否有效：<select name="_isvalid">
                            <option value="1" <%=user.getIsValid() == 1 ? "selected = 'selected'" : ""%>>有效</option>
                            <option value="0" <%=user.getIsValid() == 0 ? "selected = 'selected'" : ""%>>无效</option>
                        </select></li>
                    </ul>
                        <input type="submit" value="提交" id="_submit"/>
                </div>
                <div class="man-right" >
                    <%
                        String type = Format.null2Blank(request.getAttribute("type"));
                        StringBuilder sb = new StringBuilder();
                        if (type.equals("select")) {
                            try {
                                List<User> list = (List<User>) request.getAttribute("userlist");
                                sb.append("<div class='r-1-bot'>");
                                sb.append("<p class='r-1-name'>姓名</p>");
                                sb.append("<p class='r-1-name'>类型</p>");
                                sb.append("<p class='r-1-name'>缺席次数</p>");
                                sb.append("<p class='r-1-name'>操作</p>");
                                sb.append("</div>");
                                for (User _user : list) {
                                    sb.append("<div class='r-1-bot'>");
                                    sb.append("<p class='r-1-name'>");
                                    sb.append(_user.getName());
                                    sb.append("</p>");
                                    sb.append("<p class='r-1-name'>");
                                    sb.append(Format.null2Blank(request.getAttribute("usertype")));
                                    sb.append("</p>");
                                    sb.append("<p class='r-1-name'>");
                                    sb.append(_user.getAbsenceNum());
                                    sb.append("</p>");
                                    sb.append("<p class='r-1-name'>");
                                    sb.append("<a href='javascript:edit(");
                                    sb.append(_user.getId());
                                    sb.append(")' >编辑</a>&nbsp;&nbsp;&nbsp;<a href='javascript:del(");
                                    sb.append(_user.getId());
                                    sb.append(")' >删除</a></p>");
                                    sb.append("</div>");
                                }
                            } catch (Exception e) {
                            }
                        }
                        out.print(sb.toString());
                    %>
                </div>
            </form>
        </div>
    </body>
</html>


